package top.xinsix.est.service.impl;
import top.xinsix.est.mapper.SmsLogMapper;
import top.xinsix.est.pojo.SmsLog;
import top.xinsix.est.service.SmsLogService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import top.xinsix.est.dto.Page;
import top.xinsix.est.constants.Constants;
import top.xinsix.est.tools.EmptyUtils;
import top.xinsix.est.tools.MathUtils;
import top.xinsix.est.tools.redis.RedisKeyUtils;
import top.xinsix.est.tools.redis.RedisUtils;

import javax.annotation.Resource;

@Service
public class SmsLogServiceImpl implements SmsLogService {

    @Resource
    private SmsLogMapper smsLogMapper;

    @Resource
    private RedisUtils redisUtils;

    public SmsLog getSmsLogById(Long id){
        return smsLogMapper.getSmsLogById(id);
    }

    public List<SmsLog>	getSmsLogListByMap(Map<String,Object> param){
        return smsLogMapper.getSmsLogListByMap(param);
    }

    public Integer getSmsLogCountByMap(Map<String,Object> param){
        return smsLogMapper.getSmsLogCountByMap(param);
    }

    public Integer qdtxAddSmsLog(SmsLog smsLog){
            smsLog.setCreatdTime(new Date());
            return smsLogMapper.insertSmsLog(smsLog);
    }

    public Integer qdtxModifySmsLog(SmsLog smsLog){
        smsLog.setUpdatedTime(new Date());
        return smsLogMapper.updateSmsLog(smsLog);
    }

    public Integer qdtxDeleteSmsLogById(Long id){
        return smsLogMapper.deleteSmsLogById(id);
    }

    public Integer qdtxBatchDeleteSmsLog(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return smsLogMapper.batchDeleteSmsLog(param);
    }

    public Page<SmsLog> querySmsLogPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = smsLogMapper.getSmsLogCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SmsLog> smsLogList = smsLogMapper.getSmsLogListByMap(param);
        page.setList(smsLogList);
        return page;
     }

    public boolean sendMessage(String phone, Integer codeType, Long userId) {
        String code= MathUtils.random();   //生成随机数 4位
        String msgResult="OK"; //结果
        try {
            // boolean result=sendSmsUtils.sendSms(phone,code);
            boolean result=true;
            if(result){//发送成功，
                //将验证码存在redis中  est:0:1303235454
                String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_SMS, codeType.toString(), phone);
                //存储String类型
                redisUtils.putValue(key,code,Constants.Duration.MINUTE_INT);
            }
            //往数据库sms_log添加一条信息 phone, codeType, sendCode, msgResult, status, uId
            // Integer codeType, String phone, String message, String failInfo, Integer status, Long createdUserId
            SmsLog smsLog =new SmsLog(codeType,phone,code,msgResult,Constants.Sms.STATUS_SEND_FAILED,userId);
            smsLog.setCreatdTime(new Date());
            smsLog.setIsDelete(0);
            smsLogMapper.insertSmsLog(smsLog);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
